package nought.com.relaceelementinhtmldemo;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;// 自定义的ViewPager适配器

import java.util.List;

public class MyViewPagerAdapter extends PagerAdapter {
    private List<YourObject> objects;

    public MyViewPagerAdapter(List<YourObject> objects) {
        this.objects = objects;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
//
//    @NonNull
//    @Override
//    public Object instantiateItem(@NonNull ViewGroup container, int position) {
//        View view = inflater.inflate(R.layout.your_layout, container, false);
//        // 填充视图
//        container.addView(view);
//        return view;
//    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}

//// 在Activity或Fragment中设置ViewPager的适配器
//ViewPager viewPager = findViewById(R.id.viewpager);
//MyViewPagerAdapter adapter = new MyViewPagerAdapter(yourObjectList);
//viewPager.setAdapter(adapter);